﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TjZheng.Framework;

namespace TjZheng.Example.MVC.Controllers
{
    public class JcropController : Controller
    {
        /// <summary>
        /// 创建水印
        /// </summary>
        /// <returns></returns>
        public ActionResult RemoteImageWithWatermark()
        {
            //Image objImage = Image.FromStream(HttpHelper.CreateHttpGetStream("http://resources.gao7.com/syzone/Content/szq/lscs/images/uploads/body_bg.jpg"));
            var objStrenm = HttpHelper.CreateHttpGetStream("http://filelx.gao7.com/g1/M00/49/E4/CikzRVSvmjaAXjBmAAA3C6cPJ1Q821.jpg");
            //var objStrenm = HttpHelper.CreateHttpGetStream("http://tjzheng.example.com/Content/images/test.jpg");
            Image objImage = Image.FromStream(objStrenm);
            Image objWatermarkImg = Image.FromStream(HttpHelper.CreateHttpGetStream("http://cdn-img.easyicon.net/image/mix.png"));
            objImage.CreateWatermark(objWatermarkImg, objImage.Width - objWatermarkImg.Width, objImage.Height - objWatermarkImg.Height);
            //objImage.CreateWatermark("垃圾陈啊", 0, 0, Color.Red, new Font("Arial", 30f));
            //objImage.CreateWatermark("垃圾陈啊", System.Web.UI.WebControls.HorizontalAlign.Center, System.Web.UI.WebControls.VerticalAlign.Bottom, Color.Red, new Font("Arial", 30f));
            using (MemoryStream objMemoryStream = new MemoryStream())
            {
                objImage.Save(objMemoryStream, ImageFormat.Jpeg);
                return File(objMemoryStream.ToArray(), "image/jpeg");
            }


        }

        /// <summary>
        /// 抓取远程图片
        /// </summary>
        /// <returns></returns>
        public ActionResult RemoteImage()
        {
            return File(HttpHelper.CreateHttpGetStream("http://resources.gao7.com/syzone/Content/szq/lscs/images/uploads/body_bg.jpg"), "image/jpeg");
            //return File(HttpHelper.CreateHttpGetStream("http://filelx.gao7.com/g1/M00/49/E4/CikzRVSvmjaAXjBmAAA3C6cPJ1Q821.jpg"), "image/jpeg");
        }

        /// <summary>
        /// 压缩远程图片
        /// </summary>
        /// <returns></returns>
        public ActionResult PressRemoteImage()
        {
            Image objImage = Image.FromStream(HttpHelper.CreateHttpGetStream("http://resources.gao7.com/syzone/Content/szq/lscs/images/uploads/body_bg.jpg"));
            using (MemoryStream objMemoryStream = new MemoryStream())
            {
                //CreateThumbnail(objImage, 80).Save(objMemoryStream, ImageFormat.Jpeg);
                objImage.CreateThumbnail(60).Save(objMemoryStream, ImageFormat.Jpeg);
                return File(objMemoryStream.ToArray(), "image/jpeg");
            }
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult SubmitCut(int x, int y, int w, int h)
        {
            var objFile = Request.Files["imgfile"];
            if (objFile == null || objFile.ContentLength <= 0)
            {
                return Content("上传文件不能为空");
            }
            Image objImage = Image.FromStream(objFile.InputStream);
            using (MemoryStream objMemoryStream = new MemoryStream())
            {
                CreateCutImage(objImage, x, y, w, h).Save(objMemoryStream, ImageFormat.Jpeg);
                return File(objMemoryStream.ToArray(), "image/jpeg");
            }
        }
        /// <summary>
        /// 创建截图
        /// </summary>
        /// <param name="original"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public Image CreateCutImage(Image original, int x, int y, int width, int height)
        {
            if (width <= 0)
            {
                throw new ArgumentNullException("width", "缩略图宽度必须大于0");
            }
            if (height <= 0)
            {
                throw new ArgumentNullException("height", "缩略图高度必须大于0");
            }
            Bitmap image = new Bitmap(width, height);
            Graphics graphics = Graphics.FromImage(image);
            graphics.InterpolationMode = InterpolationMode.HighQualityBilinear;
            graphics.SmoothingMode = SmoothingMode.HighQuality;
            graphics.Clear(Color.Transparent);
            graphics.DrawImage(original, new Rectangle(0, 0, image.Width, image.Height), x, y, width, height, GraphicsUnit.Pixel);
            graphics.Dispose();
            return image;
        }
        /// <summary>
        /// 创建缩略图（宽高限制）
        /// </summary>
        /// <param name="original"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public Image CreateThumbnail(Image original, int width, int height)
        {
            if (width <= 0)
            {
                throw new ArgumentNullException("width", "缩略图宽度必须大于0");
            }
            if (height <= 0)
            {
                throw new ArgumentNullException("height", "缩略图高度必须大于0");
            }
            Bitmap image = new Bitmap(width, height);
            Graphics graphics = Graphics.FromImage(image);
            graphics.CompositingQuality = CompositingQuality.HighQuality;
            graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            graphics.SmoothingMode = SmoothingMode.HighQuality;
            graphics.Clear(Color.Transparent);
            graphics.DrawImage(original, new Rectangle(0, 0, image.Width, image.Height), 0, 0, original.Width, original.Height, GraphicsUnit.Pixel);
            graphics.Dispose();
            return image;
        }
        /// <summary>
        /// 创建缩略图（比例缩放）
        /// </summary>
        /// <param name="original"></param>
        /// <param name="percentage"></param>
        /// <returns></returns>
        public Image CreateThumbnail(Image original, int percentage)
        {
            if (percentage < 1)
            {
                throw new ArgumentNullException("percentage", "缩放比例不能小于1%");
            }
            return CreateThumbnail(original, ((int)((original.Width * 0.01f) * percentage)), ((int)((original.Height * 0.01f) * percentage)));
        }

        public ActionResult PressUploadImage()
        {
            return View();
        }

        public ActionResult UploadImage(HttpPostedFileWrapper objImageFile)
        {
            if (objImageFile == null || objImageFile.ContentLength <= 0)
            {
                return Content("上传图片不能为空");
            }
            Image objUpload = Image.FromStream(objImageFile.InputStream);
            int percent = 30;
            if (objImageFile.ContentLength > 3145728)
            {
                percent = 25;
            }
            else if (objImageFile.ContentLength < 2097152)
            {
                percent = 40;
            }
            else
            {
                percent = 30;
            }
            Image objImage = ImageHelper.CreateThumbnail(Image.FromStream(objImageFile.InputStream), percent);
            using (MemoryStream objMemoryStream = new MemoryStream())
            {
                objImage.Save(objMemoryStream, ImageFormat.Jpeg);
                return File(objMemoryStream.ToArray(), "image/jpeg");
            }
        }
    }
}
